<template>
  <div class="skuPrint">
    <ul class="query">
      <li class="query-item">
        <span>商品ID：</span>
        <el-input v-model="queryData.goodsId" placeholder="请输入商品ID" size="medium"></el-input>
      </li>
      <li class="query-item">
        <span>ID生成时间：</span>
        <el-date-picker type="date" v-model="queryData.startTime" value-format="yyyy-MM-dd" placeholder="选择日期" size="medium">
        </el-date-picker>
        -
        <el-date-picker type="date" v-model="queryData.endTime" value-format="yyyy-MM-dd" placeholder="选择日期" size="medium">
        </el-date-picker>
      </li>
      <!-- <li class="query-item">
        <span>SKU编码：</span>
        <el-input v-model="queryData.skuNo" placeholder="请输入SKU编码" size="medium"></el-input>
      </li> -->

      <li class="query-item">
        <el-button type="primary" size="medium" @click="query">查询</el-button>
        <el-button type="warning" size="medium" @click="addDialogShow">新增商品ID</el-button>
        <el-button type="primary" size="medium" @click="print">打印</el-button>
        <el-button type="primary" size="medium" @click="exportIds">导出</el-button>
      </li>
    </ul>
    <div class="wrapper">
      <el-table class="table" :data="tableData" stripe highlight-current-row border @selection-change="changeSelection">
        <el-table-column align="center" type="selection" width="50">
        </el-table-column>
        <el-table-column align="center" type="index" label="序号" width="50">
        </el-table-column>
        <el-table-column align="center" prop='create_time' label="ID生成时间">
        </el-table-column>
        <el-table-column align="center" prop="goods_id" label="商品ID">
        </el-table-column>
        <el-table-column align="center" prop="winery" label="酒厂">
        </el-table-column>
        <el-table-column align="center" prop="product_num" label="生产批号">
        </el-table-column>
        <el-table-column align="center" prop="feed_time" label="投料时间">
        </el-table-column>
        <el-table-column align="center" prop="distill_time" label="蒸馏出酒">
        </el-table-column>
        <el-table-column align="center" prop="product_time" label="灌装成品">
        </el-table-column>
      </el-table>
      <el-pagination background layout="total,sizes,prev, pager, next,jumper" :page-size="page.pageSize" :page-sizes="[10,30,50,80,100]" :total="page.total" :current-page="page.current" @size-change="handleSizeChange" @current-change="handleCurrentChange">
      </el-pagination>
    </div>
    <el-dialog title="新增ID" width="40%" :visible.sync="addData.show">
      <ul class="dialog-list">
        <li class="dialog-item">SKU编号： {{addData.skuNo}}</li>
        <li class="dialog-item">
          <span>ID数量：</span>
          <el-input-number v-model="addData.idPcs" size="small" controls-position="right" :min="0"></el-input-number>
        </li>
        <li class="dialog-item">
          <span>投料时间：</span>
          <el-date-picker type="datetime" v-model="addData.feedTime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期" size="small"></el-date-picker>
        </li>
        <li class="dialog-item">
          <span>蒸馏出酒：</span>
          <el-date-picker type="datetime" v-model="addData.distillTime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期" size="small"></el-date-picker>
        </li>
        <li class="dialog-item">
          <span>灌装成品：</span>
          <el-date-picker type="datetime" v-model="addData.productTime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期" size="small"></el-date-picker>
        </li>
        <li class="dialog-item">
          <span>酒厂：</span>
          <el-select size="small" v-model="addData.winery">
            <el-option labe="原态农业" value="原态农业"></el-option>
          </el-select>
        </li>
        <li class="dialog-item">
          <span>生产批号：</span>
          <el-input type="text" size="small" v-model="addData.productNum" placeholder="请输入生产批号"></el-input>
        </li>
      </ul>
      <div slot="footer">
        <el-button type="primary" @click="addSubmit" size="medium">确认</el-button>
        <el-button @click="addData.show = false" size="medium">关闭</el-button>
      </div>
    </el-dialog>
    <div id="print" style="display:none">
      <div v-for="(item,index) in selectData" :key="index" style="font-family:'黑体';font-weight:bold;page-break-after:always;">
        <h3 style="width:100%;font-size: 22px; text-align: center; padding-bottom: 8px;border-bottom: 2px solid #333;">温州古廊桥酒业有限公司</h3>
        <ul style="padding-left:20px; margin: 8px 0;line-height: 24px;">
          <li>
            <span style="display: inline-block;width:80px;margin-right: 15px;">商品名称：</span>
            <span>{{item.goods_name}}</span>
          </li>
          <li>
            <span style="display: inline-block;width:80px;margin-right: 15px;">SKU编码：</span>
            <span>{{item.sku_no}}</span>
          </li>
          <li>
            <span style="display: inline-block;width:80px;margin-right: 15px;">生产日期：</span>
            <span>{{item.product_time}}</span>
          </li>
          <li>
            <span style="display: inline-block;width:80px;margin-right: 15px;">生产批号：</span>
            <span>{{item.product_num}}</span>
          </li>
        </ul>
        <div style="width:320px; display: flex; justify-content: space-between;align-items:center;padding:0 10px;">
          <div style="height:82px;display:flex;flex-direction:column;align-items:center;">
            <vue-barcode :value="item.goods_id" :options="barcode_option" tag="img" style="width:180px;height:68px;"></vue-barcode>
            <p>{{item.goods_id}}</p>
          </div>
          <vue-qr :text="item.sourceLink" width="80" height="80" :size="80" :margin="0"></vue-qr>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import VueQr from 'vue-qr'
import VueBarcode from '@xkeshi/vue-barcode';
export default {
  data() {
    return {
      barcode_option: {
        displayValue: false, //是否默认显示条形码数据
        textPosition: 'bottom', //条形码数据显示的位置
        background: '#fff', //条形码背景颜色
        valid: function (valid) {
        },
        width: '2px',
        height: '68px',
        margin: 0,
        fontSize: '18px' //字体大小
      },
      exportData: {
        skuNo: this.$route.query.sku_nu,
        goodsId: ''
      },
      selectData: [],
      queryData: {
        startTime: "",
        endTime: "",
        goodsId: "",
      },
      tableData: [],
      addData: {
        show: false,
        skuNo: this.$route.query.sku_nu,
        idPcs: 0,
        feedTime: "",
        distillTime: "",
        winery: "",
        productTime: "",
        productNum: ""
      },
      page: {
        pageSize: 10,
        current: 1,
        total: 0
      }
    };
  },
  mounted() {
    this.getGoodIds();
  },
  methods: {
    changeSelection(val) {
      this.selectData = val;
    },
    print() {
      if (!this.selectData.length) {
        return this.$message.error('请先勾选打印项');
      }
      let subOutputRankPrint = document.getElementById('print');
      let newContent = subOutputRankPrint.innerHTML;
      let oldContent = document.body.innerHTML;
      document.body.innerHTML = newContent;
      window.print();
      window.location.reload();
      document.body.innerHTML = oldContent;
      return false;
    },
    exportIds() {
      if (!this.selectData.length) {
        return this.$message.error('请先勾选导出项');
      } else {
        let arr = [];
        this.selectData.forEach((item) => {
          arr.push(item.goods_id);
        })
        this.exportData.goodsId = arr.join(",");
        this.$confirm("确认进行导出操作吗？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.$post({
            api: "/import/chooseGoods",
            data: this.exportData
          }).then(({ data }) => {
            if (data.code === 0) {
              let link = document.createElement("a");
              link.href = url + data.data.replace("static/", "");
              link.download = 'sku_id';
              link.click();
            } else {
              this.$message({
                type: "error",
                message: data.msg
              });
            }
          });
        }).catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
      }
    },
    async getGoodIds() {
      let { data } = await this.$post({
        api: "/show/allgoodsid",
        data: {
          skuNo: this.$route.query.sku_nu,
          startTime: this.queryData.startTime,
          endTime: this.queryData.endTime,
          goodsId: this.queryData.goodsId,
          current: this.page.current,
          pageSize: this.page.pageSize
        }
      });
      if (data.code === 0) {
        this.tableData = data.data;
        this.page.total = data.rdPage.total;
      } else {
        this.$message({
          type: "error",
          message: data.msg
        });
      }
    },
    query() {
      this.page.current = 1;
      this.getGoodIds();
    },
    handleCurrentChange(val) {
      this.page.current = val;
      this.getGoodIds();
    },
    handleSizeChange(val) {
      this.page.current = 1;
      this.page.pageSize = val;
      this.getGoodIds();
    },
    addDialogShow() {
      this.addData = {
        show: true,
        skuNo: this.$route.query.sku_nu,
        idPcs: 0,
        feedTime: "",
        distillTime: "",
        winery: "",
        productTime: "",
        productNum: ""
      };
    },
    async addSubmit() {
      for (var attr in this.addData) {
        if (!this.addData[attr]) {
          this.$message({
            type: "error",
            message: "请先填写新增项！"
          });
          return;
        }
      }
      let { data } = await this.$post({
        api: "/add/goodsid",
        data: this.addData
      });
      if (data.code === 0) {
        this.addData.show = false;
        this.getGoodIds();
      } else {
        this.$message({
          type: "error",
          message: data.msg
        });
      }
    }
  },
  components: {
    VueBarcode,
    VueQr,
  }
};
</script>
<style lang="scss" scoped>
.query {
  overflow: hidden;
  padding: 15px 0;
  border-bottom: 1px solid #eeeeee;
  &-item {
    float: left;
    margin: 15px 20px;
    span {
      display: inline-block;
      width: 90px;
      text-align: right;
    }
  }
  .el-input {
    width: 200px;
  }
  .el-button {
    margin-left: 10px;
    &:last-child {
      margin-right: 0;
    }
  }
}
.wrapper {
  padding: 15px;
}
.dialog-list {
  overflow: hidden;
  li {
    display: flex;
    float: left;
    width: 50%;
    margin: 15px 0;
    padding: 0 15px;
    box-sizing: border-box;
    &:first-child {
      width: 100%;
    }
    span {
      flex: 0 0 70px;
      text-align: right;
      margin-right: 5px;
    }
    .el-input-number,
    .el-input,
    .el-select {
      flex: 1;
    }
  }
}
</style>